package com.exam.dao.exam;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import com.exam.common.vo.ExamGradeVO;
import com.exam.common.vo.ExamTiVO;
import com.exam.dao.base.BaseDaoImpl;
import com.exam.entity.ExamGrade;
import com.exam.entity.ExamTi;

@Repository("ExamDaoImp")
public class ExamDaoImp extends BaseDaoImpl<ExamGrade> implements ExamDao{

	
	@Override
	public void updateGrade(ExamTiVO ti,ExamGradeVO grade) {
		String hql = "update ExamGrade set examGradePoint=:point, examGradeRight=:right "
				+ "where examStuId=:stuId and examSubjectId=:subjectId";
		
			this.getSession().createQuery(hql)
			.setParameter("point", ti.getExamTiPoint())
			.setParameter("right", grade.getExamGradeRight())
			.setParameter("stuId", grade.getExamStuId())
			.setParameter("subjectId", grade.getExamSubjectId())
			.executeUpdate();
	}

	@Override
	public ExamTiVO getTi(Integer examTiDifficulty, List<Integer> list) {
		StringBuffer sb = new StringBuffer();
		sb.append(" from ExamTi where examTiDifficulty=:examTiDifficulty ");
		if(list!=null){
			sb.append(" and examTiId not in (:tiIdList) ");
		}
		sb.append(" order by rand() ");
		
		ExamTi examTi = new ExamTi();
		
		if(list == null){
			examTi = (ExamTi) this.getSession()
					.createQuery(sb.toString())
					.setParameter("examTiDifficulty", examTiDifficulty)
					.setMaxResults(1)
					.uniqueResult();
		}else{
			examTi = (ExamTi) this.getSession()
					.createQuery(sb.toString())
					.setParameter("examTiDifficulty", examTiDifficulty)
					.setParameterList("tiIdList", list)
					.setMaxResults(1)
					.uniqueResult();
		}
		
		ExamTiVO tiVO = new ExamTiVO();
		if(examTi != null){
			String tiName = examTi.getExamTiName().replace("\n", "<br/>");
			examTi.setExamTiName(tiName);
			BeanUtils.copyProperties(examTi, tiVO);
		}
		return tiVO;
	}


}
